package com.kingmed.kmss.common.utils;

import com.google.common.base.CharMatcher;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 
 */
public class StringUtil {

	/**
	 * 编码UTF8
	 * @param str
	 * @return
	 */
	public static String encodeUtf8(String str){
		String name = null;
		try {
			name = URLEncoder.encode(str, StandardCharsets.UTF_8.toString());
		} catch (UnsupportedEncodingException e) {
			name = str;
		}
		return name;
	}

	/**
	 * 按照给定的字符，拆分字符串，并将结果组成字符数组返回
	 * 
	 * @param str
	 * @param seperatorChar
	 * @return
	 */
	public static ArrayList<String> split(String str, String seperatorChar) {
		return Lists.newArrayList(Splitter.on(seperatorChar).trimResults().omitEmptyStrings().split(str));
	}

	/**
	 * 将字符数组拼接成一个字符串，并用指定的字符分割。
	 * 
	 * @param strSet
	 * @param seperatorChar
	 * @return
	 */
	public static String join(ArrayList<String> strSet, String seperatorChar) {
		Joiner joiner = Joiner.on(seperatorChar).skipNulls();
		return joiner.join(strSet);
	}

	public static String join(List<String> strSet, String seperatorChar) {
		Joiner joiner = Joiner.on(seperatorChar).skipNulls();
		return joiner.join(strSet);
	}

	/**
	 * 是否包含指定的字符串
	 * 
	 * @param str
	 * @param match
	 * @return
	 */
	public static boolean contain(String str, String match) {
		// CharMatcher.
		return str.contains(match);
	}

	/**
	 * 从字符串中删除某个字符
	 * 
	 * @param fullString
	 * @param excludeChar
	 * @return String
	 */
	public static String removeCharFromString(String fullString, char excludeChar) {
		return CharMatcher.is(excludeChar).removeFrom(fullString);
	}

	/**
	 * 从字符串中删除某个字符
	 * 
	 * @param fullString
	 * @param excludeChar
	 * @return String
	 */
	public static String trimTrailingFrom(String fullString, char excludeChar) {
		return CharMatcher.is(excludeChar).trimTrailingFrom(fullString);
	}
	/**
	 * 字节数组转对象
	 * 
	 * @param byteArray
	 * @return
	 */
	public static Object transformByteArrayToObject(byte[] byteArray) {
		Object obj = null;
		try {
			ByteArrayInputStream bis = new ByteArrayInputStream(byteArray);
			ObjectInputStream ois = new ObjectInputStream(bis);
			obj = ois.readObject();
			ois.close();
			bis.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		} catch (ClassNotFoundException ex) {
			ex.printStackTrace();
		}
		return obj;
	}

	public static String getUuid() {
		return UUID.randomUUID().toString().trim().replaceAll("-", "");
	}
}
